package com.common.sdk.net.download.request;

import java.io.File;

import com.common.sdk.net.download.http.DownloadRetryPolicy;
import com.common.sdk.net.download.request.model.DownloadInfo;
import com.common.sdk.net.connect.http.DaylilyRequest;
import com.common.sdk.net.connect.http.RetryPolicy;

/**
 * 下载的所有数据
 * 
 * @author wenlonglv
 * @param <T>
 */
public class DownloadRequest extends DaylilyRequest implements Comparable<DownloadRequest> {

    /** The retry policy for this request. */
    private RetryPolicy mRetryPolicy;
    private DownloadInfo myDownloadInfo;

    public DownloadRequest(DownloadInfo model) {
        this(model, model.getDownloadPath());
    }

    public DownloadRequest(DownloadInfo model, String url) {
        this(model, url, Method.GET);
    }

    public DownloadRequest(DownloadInfo model, String url, int method) {
        super(url, method);
        myDownloadInfo = model;
    }

    public DownloadInfo getDownloadInfo() {
        return myDownloadInfo;
    }

    /**
     * 是否是文件下载
     */
    @Override
    public boolean isFileDownload() {
        return true;
    }

    /** 下载的文件类型 */
    public int getType() {
        return myDownloadInfo.getType();
    }

    /**
     * SD卡下班是否有文件
     * 
     * @return
     */
    public boolean isHasSDFile() {
        return myDownloadInfo.isHasSDFile();
    }

    @Override
    public RetryPolicy getRetryPolicy() {
        if (mRetryPolicy == null) {
            mRetryPolicy = new DownloadRetryPolicy();
        }
        return mRetryPolicy;
    }

    /**
     * 用下载文件路径或者文件名计算hash码，带后缀的文件路径
     * 
     * @return
     */
    public File getDownloadFilePath() {
        return myDownloadInfo.getDownloadFilePath();
    }

    @Override
    public String toString() {
        return getClass().getName() + '@' + Integer.toHexString(hashCode());
    }

    public boolean isDownloadingDBState() {
        return myDownloadInfo.isDownloadingDBState();
    }

    public boolean isWaitDownload() {
        return myDownloadInfo.isWaitDownload();
    }

    public boolean isStartDownload() {
        return myDownloadInfo.isStartDownload();
    }

    public boolean isPauseDownload() {
        return myDownloadInfo.isPauseDownload();
    }

    public boolean isStopDownload() {
        return myDownloadInfo.isStopDownload();
    }

    public boolean isDeleteDownload() {
        return myDownloadInfo.isDeleteDownload();
    }

    public boolean isFinishDownload() {
        return myDownloadInfo.isFinishDownload();
    }

    public boolean isSilenceDownloadPause() {
        return myDownloadInfo.isSilenceDownloadPause();
    }

    public void pauseSilenceDownload() {
        myDownloadInfo.pauseSilenceDownload();
    }

    public void unPauseSilenceDownload() {
        myDownloadInfo.unPauseSilenceDownload();
    }

    @Override
    public int compareTo(DownloadRequest absRequest) {
        return (int) (DownloadRequest.this.getDownloadInfo().getRequestStartTime() - absRequest.getDownloadInfo()
                .getRequestStartTime());
    }

}
